<?php
	require '../../global.php';
	$update_path = $_POST['path'];
	$date = $_POST['date'];
	$hour = $_POST['hour'];
	$min = $_POST['min'];
	$html = $_POST['html'];
	$time_str = "{$date} {$hour}:{$min}";
	$time = strtotime($time_str);
	
	$exp_dir = $_POST['exp_dir'];
	
	$dir = ROOT;
	$i = 0;
	function pick_new_file($dir){
		global $exp_dir, $time, $i, $update_path;
		$handle = opendir($dir);
		while (false !== ($file = readdir($handle))) {
			if($file != '.' && $file !== '..') {
				foreach ($exp_dir as $v){
					if($file == $v)
						continue 2;
				}
				
				$cur_path = $dir .'/'. $file;
				if(is_dir($cur_path)) {
					pick_new_file($cur_path);
				}else{
					$ft = filemtime($cur_path);
					if($ft>$time){
						$root_length = strlen(ROOT);
						$re_path = substr($cur_path, $root_length);
						$last_index = strripos($re_path,'/');
						if($last_index!=-1){
							$re_path = substr($re_path, 0, $last_index);
						}else{
							$re_path = '';
						}
						$save_path = $update_path . '/'.$re_path;
//						echo $save_path.'<br>';
						mkdirs($save_path);
						if(!file_exists($save_path)){
							mkdir($save_path);
						}
						$i ++ ;
						pick_file($cur_path,$save_path.'/'.$file);
					}
				}
			}
		}
		closedir($handle);
		
	}
	
	function mkdirs($dir){  
		if(!is_dir($dir))  
		{  
			if(!mkdirs(dirname($dir))){  
				return false;  
			}  
			if(!mkdir($dir)){  
				return false;  
			}  
		}  
		return true;  
	}
	
	function pick_file($file_name, $save_path){
		//复制文件
		copy($file_name, $save_path);
	}
	
	pick_new_file(ROOT);
	
	if($html!='0'){
		pick_new_file(ROOT.'/html/'.$html);
	}
	
	echo '共创建文件：'. $i.'个';
	echo '<script >window.location.href="/tool/index.php";</script>';